package com.ossjk.qlh.tgb.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.tgb.dto.ActivityStatusDto;
import com.ossjk.qlh.tgb.entity.*;
import com.ossjk.qlh.tgb.service.*;
import com.ossjk.qlh.tgb.vo.ActivityInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright  Sat Jan 22 21:51:38 CST 2022 QLH. Tech Ltd. All rights reserved.
 * 
 * @Package: com.ossjk.qlh.projects
 * @ClassName: ActivityInfoController
 * @Description: ActivityInfo-控制器
 * @author: chair
 * @date:  2022-01-22 21:51:39 
 */
@Api(tags = "活动商品信息")
@RestController
@RequestMapping("/tgb/activity")
public class ActivityInfoController extends BaseController {
	
	@Autowired
	private IActivityInfoService iActivityInfoService;
	@Autowired
	private IActivityUserService iActivityUserService;
	@Autowired
	private IRetailUserService iRetailUserService;
	@Autowired
	private IRetailStoreService iRetailStoreService;
	@Autowired
	private IProductDetailsService iProductDetailsService;

	@ApiOperation(value = "列表")
	@RequiresPermissions("00300020")
	@GetMapping("/list")
	public ResponseBean<Page<ActivityInfo>> list(
			@ApiParam(value = "商品名称") @RequestParam(name = "productName", required = false) String productName,
			@ApiParam(value = "活动类型") @RequestParam(name = "activityType", required = false) String activityType,
			@ApiParam(value = "活动状态") @RequestParam(name = "status", required = false) String status,
			@ApiParam(value = "最少参与人数") @RequestParam(name = "least", required = false) String least,
			@ApiParam(value = "最多参与人数") @RequestParam(name = "more", required = false) String more,
			Page page, HttpServletRequest request) {
		QueryWrapper<ActivityInfo> queryWrapper = new QueryWrapper<ActivityInfo>();
		if (StrUtil.isNotBlank(productName)){
			queryWrapper.like("pd.name",productName);
		}
		if (StrUtil.isNotBlank(activityType)){
			queryWrapper.like("type",activityType);
		}
		if (StrUtil.isNotBlank(status)){
			queryWrapper.eq("status",status);
		}
		if (StrUtil.isNotBlank(least)){
			queryWrapper.lt("user_count",least);
		}
		if (StrUtil.isNotBlank(more)){
			queryWrapper.gt("user_count",more);
		}
		if (StrUtil.isNotBlank(getBusinessId(request))) {
			queryWrapper.eq("business_id", getBusinessId(request));
		}
		queryWrapper.eq("is_delete",0);

		Page pageVo = iActivityInfoService.getActivityInfoPage(queryWrapper,page);

		return ResponseBean.Success(pageVo);
	}


	@ApiOperation(value = "去添加")
	@RequiresPermissions("003000200005")
	@GetMapping("/toSave")
	public ResponseBean<Map<String,Object>> toSave() {
		Map<String,Object> data = new HashMap<>();
		List<RetailUser> businessList = iRetailUserService.getBusinessList();
		if (ObjectUtil.isNotEmpty(businessList)){
			data.put("businessList",businessList);
		}
		return ResponseBean.Success(data);
	}

	@ApiOperation(value = "添加")
	@RequiresPermissions("003000200005")
	@PostMapping("/save")
	public ResponseBean save(@RequestBody ActivityInfo record) {
		if (iActivityInfoService.save(record)) {
			return ResponseBean.Success();
		} else {
			return ResponseBean.Fail();
		}
	}


	@ApiOperation(value = "根据商户id获取店铺")
	@RequiresPermissions("003000200005")
	@GetMapping("/getStoreByBusinessId")
	public ResponseBean getStoreByBusinessId(String businessId){
		QueryWrapper<RetailStore> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("business_id",businessId);
		queryWrapper.eq("is_use",1);
		List<RetailStore> storeList = iRetailStoreService.list(queryWrapper);
		return ResponseBean.Success(storeList);
	}

	@ApiOperation(value = "根据店铺id获取商品")
	@RequiresPermissions("003000200005")
	@GetMapping("/getProductListByStoreId")
	public ResponseBean getProductListByStoreId(String storeId){
		QueryWrapper<ProductDetails> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("store_id",storeId);
		List<ProductDetails> productList = iProductDetailsService.list(queryWrapper);
		return ResponseBean.Success(productList);
	}



	@ApiOperation(value = "去编辑")
	@RequiresPermissions("003000200010")
	@GetMapping("/toUpdate")
	public ResponseBean<ActivityInfo> toUpdate(@ApiParam(value = "id") @RequestParam(name = "id") String id) {
		return null;
	}


	@ApiOperation(value = "编辑")
	@RequiresPermissions("003000200010")
	@PutMapping("/update")
	public ResponseBean update(@RequestBody ActivityInfo record) {
		if (iActivityInfoService.updateById(record)) {
			return ResponseBean.Success();
		} else {
			return ResponseBean.Fail();
		}
	}

	@ApiOperation(value = "删除")
	@RequiresPermissions("003000200015")
	@DeleteMapping("/remove")
	public ResponseBean remove(@ApiParam(value = "ids") @RequestParam(name = "ids") String[] ids) {
		if (iActivityInfoService.removeByIds(Arrays.asList(ids))) {
			return ResponseBean.Success();
		} else {
			return ResponseBean.Fail();
		}
	}

	@ApiOperation(value = "获取参与活动的用户")
	@RequiresPermissions("003000200020")
	@GetMapping("/getActivityUser")
	public ResponseBean<Page<ActivityUser>> getActivityUser(
			@ApiParam(value = "activityId") @RequestParam(name = "activityId", required = true)String activityId,
			Page page){
		QueryWrapper<ActivityUser> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("activity_id",activityId);
		Page userPage = iActivityUserService.getUerPageByActivityId(queryWrapper,page);
		return ResponseBean.Success(userPage);
	}

	@ApiOperation(value = "修改活动状态")
	@RequiresPermissions("003000200010")
	@PutMapping("/updateStatus")
	public ResponseBean updateStatus(@RequestBody ActivityStatusDto activityStatusDto){
		ActivityInfo activityInfo = iActivityInfoService.getById(activityStatusDto.getActivityId());
		activityInfo.setStatus(activityStatusDto.getStatus());
		boolean isSuccess = iActivityInfoService.updateById(activityInfo);
		if (isSuccess){
			return ResponseBean.Success();
		}
		return ResponseBean.Fail();
	}



}
